﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;

namespace Altairis.Sewen.WebApp {
    public class Global : System.Web.HttpApplication {

        protected void Application_Start(object sender, EventArgs e) {
            RouteTable.Routes.MapPageRoute("HomePage", "", "~/Sewen/Page.aspx", false, new RouteValueDictionary(new { slug = "home-page" }));
            RouteTable.Routes.MapPageRoute("SewenWikiPage", "page/{slug}", "~/Sewen/Page.aspx");
            RouteTable.Routes.MapPageRoute("SewenWikiPageEdit", "edit/{slug}", "~/Sewen/PageEdit.aspx");
            RouteTable.Routes.MapPageRoute("SewenWikiPageHistory", "history/{slug}", "~/Sewen/History.aspx");
            RouteTable.Routes.MapPageRoute("SewenWikiPageVersion", "history/{slug}/{version}", "~/Sewen/Page.aspx", false, null, new RouteValueDictionary(new { version = new VersionRouteConstraint() }));
            RouteTable.Routes.MapPageRoute("SewenWikiPageNew", "new", "~/Sewen/PageNew.aspx");
            RouteTable.Routes.MapPageRoute("SewenWikiSearch", "search", "~/Sewen/Search.aspx");
        }

        private class VersionRouteConstraint : IRouteConstraint {
            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
                var val = values[parameterName] as string;
                DateTime dt;
                return DateTime.TryParseExact(val, "yyyyMMddHHmmssfff", null, System.Globalization.DateTimeStyles.AssumeLocal, out dt);
            }
        }

    }
}